package com.ligong.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ligong.blog.common.exception.BlogException;
import com.ligong.blog.common.util.BeanTransformUtils;
import com.ligong.blog.common.util.JwtUtils;
import com.ligong.blog.common.util.SecurityUtils;
import com.ligong.blog.contanst.Constants;
import com.ligong.blog.mapper.BlogInfoMapper;
import com.ligong.blog.mapper.UserInfoMapper;
import com.ligong.blog.pojo.dataobject.BlogInfo;
import com.ligong.blog.pojo.dataobject.UserInfo;
import com.ligong.blog.pojo.request.UserLoginRequest;
import com.ligong.blog.pojo.response.BlogInfoResponse;
import com.ligong.blog.pojo.response.UserInfoResponse;
import com.ligong.blog.pojo.response.UserLoginResponse;
import com.ligong.blog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private BlogInfoMapper blogInfoMapper;

    @Override
    public UserLoginResponse login(UserLoginRequest userLoginRequest) {
        UserInfo userInfo = queryUserByName(userLoginRequest.getUserName());
        if (userInfo == null || userInfo.getId() <= 0) {
            throw new BlogException("用户不存在");
        }
        // 判断密码是否正确
//        if (!userLoginRequest.getPassword().equals(userInfo.getPassword())) {
//            throw new BlogException("用户密码不正确");
//        }
        if (!SecurityUtils.verify(userLoginRequest.getPassword(), userInfo.getPassword())) {
            throw new BlogException("用户密码不正确");
        }
        // 密码正确
        Map<String, Object> claim = new HashMap<>();
        claim.put("id", userInfo.getId());
        claim.put("userName", userInfo.getUserName());
        return new UserLoginResponse(userInfo.getId(), JwtUtils.genToken(claim));
    }

    @Override
    public UserInfoResponse getUserInfo(Integer userId) {
        UserInfo userInfo = getUserInfoById(userId);
        return BeanTransformUtils.trans(userInfo);
    }

    @Override
    public UserInfoResponse getAuthorInfo(Integer blogId) {
        // 1. 获取作者的id
        BlogInfo blogInfo = getBlogInfo(blogId);
        if (blogInfo == null || blogInfo.getUserId() <= 0) {
            throw new BlogException("blogId不合法");
        }
        // 2. 根据作者id获取作者信息
        UserInfo userInfo = getUserInfoById(blogInfo.getUserId());
        return BeanTransformUtils.trans(userInfo);
    }

    public UserInfo queryUserByName(String userName) {
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserInfo::getUserName, userName)
                .eq(UserInfo::getDeleteFlag, Constants.NOT_DELETE);
        return userInfoMapper.selectOne(queryWrapper);
    }

    public UserInfo getUserInfoById(Integer userId) {
        return userInfoMapper.selectOne(new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getId, userId)
                .eq(UserInfo::getDeleteFlag, Constants.NOT_DELETE));
    }

    public BlogInfo getBlogInfo(Integer blogId) {
        return blogInfoMapper.selectOne(new LambdaQueryWrapper<BlogInfo>()
                .eq(BlogInfo::getId, blogId)
                .eq(BlogInfo::getDeleteFlag, Constants.NOT_DELETE));
    }

}
